﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_TT : Form
    {
        public Frm_TT()
        {
            InitializeComponent();
        }
        string MBan = "";
        ConvertNumToStr objConvert = new ConvertNumToStr();
        private void Frm_TT_Load(object sender, EventArgs e)
        {
            Load_LVBan();
            Load_LV_DSTD();
            Load_CBBNV();
        }
        public void Load_LVBan()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MAU_Ban();
            load_ban(tb);
        }
        public void load_ban(DataTable tb)
        {
            LV_Ban.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "1")
                    item.ImageIndex = 1;
                else
                {
                    if (tb.Rows[i][2].ToString() == "2")
                        item.ImageIndex = 2;
                }
                LV_Ban.Items.Add(item);
            }
        }
     //   #region
        public void Load_LV_DSTD()
        {

            //string MB = LV_Ban.FocusedItem.SubItems[0].Text;
            DataTable DTLV = new DataTable();
            DTLV = Cls_ThucDon.ThucDon_Ban(MBan);
            LV_DSGM.Items.Clear();
            for (int i = 0; i < DTLV.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = DTLV.Rows[i][2].ToString();
                item.SubItems.Add(DTLV.Rows[i][3].ToString());
                item.SubItems.Add(DTLV.Rows[i][4].ToString());
                item.SubItems.Add(DTLV.Rows[i][5].ToString());
                item.SubItems.Add(DTLV.Rows[i][6].ToString());
                LV_DSGM.Items.Add(item);
            }
        }
        public void Load_Tien()
        {
            int tst = 0;
            for (int i = 0; i < LV_DSGM.Items.Count; i++)
            {
                //int ts = int.Parse(LV_DSGM.Items[i].SubItems[4].Text);

                tst += int.Parse(LV_DSGM.Items[i].SubItems[4].Text);
            }
            Lbl_TS.Text = tst.ToString() + " (VNĐ)";
            Lbl_TC.Text = objConvert.converNumToString(objConvert.slipArray(tst.ToString())) + " (VNĐ)";
        }

        private void LV_Ban_MouseClick(object sender, MouseEventArgs e)
        {
            MBan = LV_Ban.FocusedItem.SubItems[0].Text;
            Load_LV_DSTD();
            string MB = LV_Ban.FocusedItem.SubItems[0].Text;
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=2");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=2 WHERE MaBan='{0}'", MB);
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
            Load_Tien();
            Load_LVBan();

            btTinhTien.Enabled = true;
        }
        public void Load_CBBNV()
        {
            DataTable tb = CLs_NhanVien.LayDSNVCBB();
            cmbDSNhanVien.DataSource = tb;

            cmbDSNhanVien.DisplayMember = "HoTen";
            cmbDSNhanVien.ValueMember = "MaNV";
        }
        private void Frm_TT_FormClosed(object sender, FormClosedEventArgs e)
        {
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=2");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
        }
        public int sohd(string maban)
        {
            string sql = string.Format("select sohd from HoaDon where MaBan='{0}'", maban);
            SqlCommand sqlcmd = new SqlCommand(sql, Coso.con);
            return (int)sqlcmd.ExecuteScalar();
        }
        private void btTinhTien_Click(object sender, EventArgs e)
        {
            string so = Convert.ToString(sohd(MBan));
            string st = string.Format("UPDATE Ban SET TinhTrang=0 WHERE MaBan='{0}'", MBan);
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            //string str = string.Format("UPDATE HoaDon SET ThanhToan='{0}' WHERE SoHD='{1}'", Lbl_TS.Text, so);
            //SqlCommand com = new SqlCommand(str, Coso.con);
            //com.ExecuteNonQuery();
            //string stri = string.Format("UPDATE ChiTietHoaDon SET TrangThai=1 WHERE SoHD='{0}'", so);
            //SqlCommand comnd = new SqlCommand(stri, Coso.con);
            //comnd.ExecuteNonQuery();
            string st_gb = string.Format("UPDATE Ban SET TinhTrang=0, GhepBan=0 WHERE TinhTrang=5 AND GhepBan='{0}'", MBan);
            SqlCommand com_gb = new SqlCommand(st_gb, Coso.con);
            com_gb.ExecuteNonQuery();
            //string st_huy = string.Format("UPDATE Ban SET TinhTrang=0, GhepBan=0 WHERE TinhTrang=5 AND GhepBan='{0}'", MBan);
            //SqlCommand com_huy = new SqlCommand(st_huy, Coso.con);
            //com_huy.ExecuteNonQuery();
            if (Cls_ThucDon.Update_TTBan(so)) { }
            Load_LVBan();
            LV_DSGM.Items.Clear();
            btTinhTien.Enabled = false;
        }
    }
}
